package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.pojo.ResultVo;
import com.usian.student.dao.StudentDao;
import com.usian.student.pojo.TbStudent;
import com.usian.student.service.StudentService;
//import com.usian.third.utils.VerifyRealName;
import com.usian.third.utils.VerifyRealName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
  private   StudentDao studentDao;

    @Override
    public ResultVo register(TbStudent tbStudent)throws Exception {
        VerifyRealName verifyRealName=new VerifyRealName();
//        Map result=verifyRealName.checkUser(tbStudent.getCardNo(),tbStudent.getPhone(),tbStudent.getName());
//        System.out.println(result);
//        if (result.get("code").equals("0")){
        if(true){
                if (true){
                    String redisCode=stringRedisTemplate.opsForValue().get(tbStudent.getPhone());
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(tbStudent.getCode())){
                            LambdaQueryWrapper<TbStudent> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                            lambdaQueryWrapper.eq(TbStudent::getPhone,tbStudent.getPhone());
                            long count=studentDao.selectCount(lambdaQueryWrapper);
                            if (count>0){
                                return  new ResultVo(300,"该用户已经被注册，不能重复注册",null);
                            }else {
                                studentDao.insert(tbStudent);
                                return new ResultVo(200,"success",null);
                            }
                        }else{
                            return  new ResultVo(300,"验证码错误",null);
                        }
                    }else{
                        return  new ResultVo(300,"验证码过期",null);
                    }
                }else {
                    return  new ResultVo(300,"异常",null);
//                        return new ResultVo(Integer.parseInt(result.get("code")+""),result.get("mag")+"",null);
                }
            }else {
                    return  new ResultVo(300,"异常",null);
//            return new ResultVo(Integer.parseInt(result.get("code")+""),result.get("mag")+"",null);
        }


    }
}
